<template>
    <div class="reg-wrap">
        <div class="ms-reg">
            <div class="ms-title">后台管理系统</div>
            <el-form :model="param" :rules="rules" ref="reg" label-width="0px" class="ms-content">
                
                <el-form-item prop="username">
                    <el-input v-model="param.username" placeholder="请输入用户名">
                        <el-button slot="prepend" icon="el-icon-lx-people"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input
                        type="password"
                        placeholder="请输入密码"
                        v-model="param.password"
                        @keyup.enter.native="submitForm()"
                    >
                        <el-button slot="prepend" icon="el-icon-lx-lock"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="phone">
                    <el-input
                        type="phone"
                        placeholder="请输入手机号"
                        v-model="param.phone"
                        @keyup.enter.native="submitForm()"
                    >
                        <el-button slot="prepend" icon="el-icon-phone"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="email">
                    <el-input
                        type="email"
                        placeholder="请输入邮箱"
                        v-model="param.email"
                        @keyup.enter.native="submitForm()"
                    >
                        <el-button slot="prepend" icon="el-icon-message"></el-button>
                    </el-input>
                </el-form-item>

                <el-select v-model="type_value" placeholder="请选择权限">
                    <el-option
                    v-for="item in type_list"
                    :key="item.id"
                    :label="item.types"
                    :value="item.id">
                    </el-option>
                </el-select>

                <el-select v-model="com_value" placeholder="请选择公司">
                    <el-option
                    v-for="item in com_list"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id">
                    </el-option>
                </el-select>



               
                
                <!-- 验证码 -->
                <!-- <el-form-item prop="code">
                    <el-input
                        type="code"
                        placeholder="code"
                        v-model="param.code"
                        @keyup.enter.native="submitForm()"
                    >
                        <el-button slot="prepend" icon="el-icon-s-order"></el-button>
                    </el-input>
                </el-form-item>
                <div class="reg-btn">
                <img :src="src" style="cursor:pointer;text-align: center;}" @click="changeimg">
                </div> -->
                <br><br>
                <div class="reg-btn">
                    <el-button type="primary" @click="submitForm()">注册</el-button>
                </div>
            </el-form>
        </div>
    </div>
</template>

<script>
import { Register,Types,comall_get } from '../../api/api.js'
export default {
    data: function() {
        return {
            src:"http://localhost:8000/code/",
            param: {
                username: '',
                password: '',
                phone:'',
                email:'',
                commpany:'',
                code:'',
                
            },
            rules: {
                username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
                password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
                phone: [{ required: true, message: '请输入手机号', trigger: 'blur' }],
                email: [{ required: true, message: '请输入邮箱', trigger: 'blur' }],
                commpany: [{ required: true, message: '请输入公司', trigger: 'blur' }],
                code: [{ required: true, message: '请输入验证码', trigger: 'blur' }],
            },

            //权限数据
            type_list:[],
            //权限值
            type_value:'',
            //公司列表
            com_list:[],
            //公司值
            com_value:'',
        };
    },
    methods: {
        //刷新验证码
        // changeimg:function(){
        //     //定义一个随机数
        //     this.src = 'http://localhost:8000/code/?random='+(Math.random()*10)
        // },
        submitForm() {
            if(this.param.username==''){
                alert('用户名不能为空')
                return false
            }
            if(this.param.password==''){
                alert('密码不能为空')
                return false
            }else if(this.param.password.length < 6){
                alert('密码不能短于6位')
                return false        
            }
            if(this.param.phone==''){
                alert('手机号不能为空')
                return false
            }
            if(!/^((13|14|15|17|18)[0-9]{1}\d{8})$/.test(this.param.phone)){
                alert('手机号格式不正确')
                return false
            }
             if(this.param.email==''){
                alert('邮箱不能为空')
                return false
            }
            if(!/^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/.test(this.param.email)){
                alert('邮箱格式不正确')
                return false
            }
            

            // 封装注册需要的数据
            var params = {
                username:this.param.username,
                password:this.param.password,
                phone:this.param.phone,
                email:this.param.email,
                type_id:this.type_value,
                company_id:this.com_value,

                
            };

            

            // 注册  
            Register(params).then(res => {
                if(res.code==200){
                    this.$message(res.message)  
                    this.$router.push('/login')  
                }else{
                     this.$message(res.message)  
                } 
            })
        },

         // 获取权限
            get_type(){
                Types().then(res=>{
                this.type_list = res.data
            })
        },
        // 获取公司
            get_com(){
                comall_get().then(res=>{
                    this.com_list = res.data
                })
        },
    },

    mounted() {
        // 调用权限请求
        this.get_type()
        // 公司请求
        this.get_com()
    },
};
</script>

<style scoped>
.reg-wrap {
    position: relative;
    width: 100%;
    height: 100%;
    background-image: url(../../assets/img/login-bg.jpg);
    background-size: 100%;
}
.ms-title {
    width: 100%;
    line-height: 50px;
    text-align: center;
    font-size: 20px;
    color: #fff;
    border-bottom: 1px solid #ddd;
}
.ms-reg {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 350px;
    margin: -190px 0 0 -175px;
    border-radius: 5px;
    background: rgba(255, 255, 255, 0.3);
    overflow: hidden;
}
.ms-content {
    padding: 30px 30px;
}
.reg-btn {
    text-align: center;
}
.reg-btn button {
    width: 100%;
    height: 36px;
    margin-bottom: 10px;
}
.reg-tips {
    font-size: 12px;
    line-height: 30px;
    color: #fff;
}
</style>